var Bmob = require("../../../../utils/bmob.js");
var util = require('../../../../utils/util.js');
var app = getApp();
var that;
Page({
  data: {
    showAllDesc: false,
    movie: {},
    remind: "加载中",
    id:"24773958",
  },
  onHide: function(){
    this.setData({ remind: ''});
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    that = this;
    var id = options.id || that.data.id;
    that.setData({ remind: '加载中', id: id});
    that.loadData(id);
  },
  loadData: function(id){
    var url = app.globalData.doubanBase + app.globalData.subject + id;
    wx.request({
      url: url,
      method: 'GET',
      header: { 'content-type': 'json' },
      success: function (res) {
        if (res.statusCode == 404) {
          that.setData({ remind: '404' });
          return;
        }
        var data = res.data;
        var readyData = {};
        var directorsAndCasts = [];
        for (var i in data.directors) {
          if (!data.directors.hasOwnProperty(i)) continue;
          directorsAndCasts.push(data.directors[i]);
        }
        for (var j in data.casts) {
          if (!data.casts.hasOwnProperty(j)) continue;
          directorsAndCasts.push(data.casts[j]);
        }
        var genres = "";
        var separate = " / ";
        for (var k in data.genres) {
          if (!data.genres.hasOwnProperty(k)) continue;
          genres += data.genres[k] + separate;
        }
        genres = genres.substring(0, genres.length - separate.length);
        var countries = "国家：";
        for (var g in data.countries) {
          if (!data.countries.hasOwnProperty(g)) continue;
          countries += data.countries[g] + separate;
        }
        countries = countries.substring(0, countries.length - separate.length);
        var photos = {};
        data.photos.forEach(function (p, index) {
          photos[index] = p['image'];
        });
        var max_title_length = 14;
        var bloopers = data.bloopers;
        if (bloopers) {
          bloopers.forEach(function (p, index) {
            p['title'] = p['title'].length < max_title_length ? p['title'] : (p['title'].substr(0, max_title_length - 2) + "...");
          });
        }
        var trailers = data.trailers;
        if (trailers) {
          trailers.forEach(function (p, index) {
            p['title'] = p['title'].length < max_title_length ? p['title'] : (p['title'].substr(0, max_title_length - 2) + "...");
          });
        }
        readyData["movie"] = {
          id: data.id,
          title: data.title,
          images: data.images,
          directorsAndCasts: directorsAndCasts,
          collectCount: data.collect_count,
          commentsCount: data.comments_count,
          wishCount: data.wish_count,
          reviewsCount: data.reviews_count,
          countries: countries,
          doCount: data.do_count,
          genres: genres,
          originalTitle: "原名：" + data.original_title,
          rating: data.rating,
          ratingsCount: data.ratings_count + "人",
          subtype: data.subtype,
          summary: data.summary || "暂无数据",
          shareUrl: data.share_url,
          year: data.year,
          photos: photos,
          bloopers: bloopers, //花絮
          trailers: trailers, //预告片
          popular_comments: data.popular_comments,
          popular_reviews: data.popular_reviews
        };
        that.setData({ remind: '' });
        that.setData(readyData);
      },
      fail: function () {
        that.setData({ remind: '404' });
      },
      complete: function () {
      }
    })
  },
  /** 查看海报 */
  bindPoster: function (event) {
    var photos = this.data.movie.photos;
    if (photos){
      var tmp = [];
      for (var p in photos){
        tmp.push(photos[p]);
      }
      wx.previewImage({
        current: tmp[event.target.dataset.id],
        urls: tmp
      })
    }
  },
  /** 展开简介   */
  handleExtensiontap: function (event) {
    var readyData = {
      "showAllDesc": true
    };
    this.setData(readyData);
  },
  /** 用户点击想看 */
  handleWishtap: function (event) {
    wx.showModal({
      title: '提示',
      content: '一起去看吧',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        }
      },
      showCancel: false
    });
  },
  /** 用户点击看过 */
  handleDotap: function (event) {
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/movie/movie-detail/rating/rating?id=' + id
    });
  },
  /** 查看影人信息 */
  handleCelebrity: function (event) {
    var id = event.currentTarget.dataset.id;
    var avatar = event.currentTarget.dataset.avatar;
    wx.navigateTo({
      url: '/pages/my/movie/movie-detail/celebrity/celebrity?id=' + id + "&&avatar=" + avatar
    });
  },
  handleReview: function (event){
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/my/movie/movie-review/review?id=' + id
    });
  },
  handleVideo: function (e) {
    var src = e.currentTarget.dataset.src;
    var poster = e.currentTarget.dataset.avatar;
    app.globalData.doubanVideoUrl = src;    
    wx.navigateTo({
      url: '/pages/my/movie/video/video?&poster=' + poster
    })
  },
  onPullDownRefresh: function(){
    var that = this;
    var id = that.data.id;
    that.setData({ remind: '加载中'});
    that.loadData(id);
  }
})